/**
 * 删除菜单
 * @url admin/system/menu/sys/delete
 * @description 删除菜单
 */
module.exports = {
	main: async (event) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _ } = util;
		let { uid } = data;
		let res = { code: 0, msg: '删除成功' };
		
		// 业务逻辑开始-----------------------------------------------------------
		let { _id } = data;
		
		if (!_id) {
			return { code: -1, msg: '请指定要删除的菜单' };
		}
		
		// 检查是否有子菜单
		let childMenus = await db.collection('opendb-admin-menus')
			.where({
				parent_id: db.command.eq(db.collection('opendb-admin-menus').doc(_id).field('menu_id'))
			})
			.get();
		
		if (childMenus.data.length > 0) {
			return { code: -1, msg: '该菜单下有子菜单，无法删除' };
		}
		
		// 不能删除系统内置菜单
		let menu = await db.collection('opendb-admin-menus').doc(_id).get();
		if (menu.data && (menu.data.menu_id === 'system_menu' || menu.data.menu_id === 'system_management')) {
			return { code: -1, msg: '不能删除系统内置菜单' };
		}
		
		// 删除菜单
		await db.collection('opendb-admin-menus').doc(_id).remove();
		
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}

